package com.globalwave.common
{

	//public class FilterComboBox extends ComboBox
	
	import com.globalwave.util.CodeUtil;
	import com.globalwave.util.U;
	
	import flash.events.Event;
	
	import mx.collections.ArrayCollection;
	import mx.collections.ICollectionView;
	import mx.collections.IViewCursor;
	import mx.collections.XMLListCollection;
	import mx.controls.Alert;
	import mx.controls.ComboBox;
	import mx.events.FlexEvent;
	import mx.utils.StringUtil;
	
	/**  
	 * ComboBox过滤自动完成组件  
	 * @author 韩敬诺   
	 *   
	 */  
	public class CodeComboBox extends ComboBox   
	{   
		public function CodeComboBox()   
		{
			this.valueField = "id" ;
			this.labelField = "name_" ;
		}   
		
		public static const SELECT_TIP_NULL : int = -1 ;
		public static const SELECT_TIP_PLS : int = 0 ;
		public static const SELECT_TIP_ALL : int = 1 ;
		public var tip:int = SELECT_TIP_PLS ;
		public var _code:String = null; 
		public var _selectedValue:String = null ;
		
		
		override public function initialize():void
		{
			if (initialized)
				return;
			
			super.initialize() ;
			
			// init dataProvider 
			var datas:XML = null ;
			try {
				datas = (CodeUtil.dicts[_code][0] as XML).copy() ;
			} catch (e:Error) {
				datas = new XML(<root></root>) ;
				trace("load code[" + _code + "] error " + e.message) ;
			}
			var firstElem:XML = <HashMap></HashMap> ;
			
			if (tip == SELECT_TIP_PLS ) {
				firstElem[this.valueField] = "";
				firstElem[this.labelField] = "请选择...";
			} else if (tip == SELECT_TIP_ALL ) {
				firstElem[this.valueField] = "";
				firstElem[this.labelField] = "全部";
			} 
			
			datas.prependChild(firstElem) ;
			super.dataProvider = datas.HashMap ;
			
			// init selectValue
			setSelectedIndex(_selectedValue) ;
		}
		
		public function set code(value:String):void {
			_code = value ;
		}
			
		override public function set dataProvider(value:Object):void
		{
			if (value is String)
			{
			    super.dataProvider = CodeUtil.dicts[value][0].HashMap ;
			} else {
				super.dataProvider = value ;
			}
		}
		
		public var valueField:String
		
		public function set selectedValue(value:String):void {	
			_selectedValue = value ;
			
			if (initialized){
				setSelectedIndex(value) ;
			} 
		}
		
		private function setSelectedIndex(value:String):void {
			
			var valueString:String = U.obj2String(value) ;
			
			var datas:XMLListCollection = (this.dataProvider as XMLListCollection) ;
			
			for(var i:int = 0; i < datas.length; i++)
			{
				var propertyValue:String = U.obj2String(datas.getItemAt(i)[this.valueField]) ;
				if(propertyValue == valueString)
				{
					this.selectedIndex = i ;
					return ;
				}
			}
			this.selectedIndex = (tip == SELECT_TIP_NULL?-1:0) ;
		
		}
		
		
		public function get selectedValue():String {
			var selectObj:Object = this.selectedItem ;
			
			if (selectObj == null) {
				return null ;
			}
			
			return U.obj2String(selectObj[this.valueField]) ;
		}
		
	}   

}